﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using FireStarter.Base;
using Habanero.BO;
using Habanero.BO.ClassDefinition;
using Habanero.Util;

namespace Firestarter.DB.Schema.BO
{
    public class DBView : BusinessObject, IMergableBo, IDBTable
    {

        public DBView(string tableName) : this()
        {
            this.TableName = tableName;
        }

        public DBView()
        {
            TableType = "DBView";
        }

        protected DBView(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }

        public virtual String SchemaName
        {
            get
            {
                return ((String)(base.GetPropertyValue("SchemaName")));
            }
            set
            {
                base.SetPropertyValue("SchemaName", value);
            }
        }
        public override string ToString()
        {
            if (string.IsNullOrEmpty(TableName))
            {
                return this.ViewID == null ? "-unnamed View-" : Convert.ToString(this.ViewID);
            }
            return this.TableName;
        }

        public DBViewColumn GetColumn(string columnName)
        {
            return Columns.FirstOrDefault(columnInfo => columnInfo.ColumnName == columnName);
        }

        public string GetClassName(string viewNamePrefix)
        {
            return GetClassNameFromViewName(viewNamePrefix);
        }
        private string GetClassNameFromViewName(string tableNamePrefix)
        {
            if (TableName == null) return "";
            string tableNameConverted = TableName.Trim();
            tableNameConverted = StringUtilities.RemovePrefix(tableNamePrefix, tableNameConverted);
            tableNameConverted = StringUtilities.Singularize(tableNameConverted);
            tableNameConverted = StringUtilities.PascalCaseTableName(tableNameConverted);
            return tableNameConverted;
        }

        public string GetClassName(string tableNamePrefix, string primaryKeyColumnSuffix)
        {
            return GetClassNameFromTableName(tableNamePrefix);
        }


        private string GetClassNameFromTableName(string tableNamePrefix)
        {
            string tableNameConverted = TableName.Trim();
            tableNameConverted = StringUtilities.RemovePrefix(tableNamePrefix, tableNameConverted);
            tableNameConverted = StringUtilities.Singularize(tableNameConverted);
            tableNameConverted = StringUtilities.PascalCaseTableName(tableNameConverted);
            return tableNameConverted;
        }

        #region Properties
        public virtual DateTime? DateCreated
        {
            get
            {
                return ((DateTime?)(base.GetPropertyValue("DateCreated")));
            }
            set
            {
                base.SetPropertyValue("DateCreated", value);
            }
        }

        public virtual String Owner
        {
            get
            {
                return ((String)(base.GetPropertyValue("Owner")));
            }
            set
            {
                base.SetPropertyValue("Owner", value);
            }
        }

        public virtual String TableName
        {
            get
            {
                return ((String)(base.GetPropertyValue("TableName")));
            }
            set
            {
                base.SetPropertyValue("TableName", value);
            }
        }

        public Guid? TableID
        {
            get { return ViewID; }
            set { this.ViewID = value; }
        }

        public virtual Guid? ViewID
        {
            get
            {
                return ((Guid?)(base.GetPropertyValue("ViewID")));
            }
            set
            {
                base.SetPropertyValue("ViewID", value);
            }
        }

        public virtual Guid? DatabaseID
        {
            get
            {
                return ((Guid?)(base.GetPropertyValue("DatabaseID")));
            }
            set
            {
                base.SetPropertyValue("DatabaseID", value);
            }
        }

        public virtual String TableType
        {
            get
            {
                return ((String)(base.GetPropertyValue("TableType")));
            }
            set
            {
                base.SetPropertyValue("TableType", value);
            }
        }

        public virtual String TableComment
        {
            get
            {
                return ((String)(base.GetPropertyValue("TableComment")));
            }
            set
            {
                base.SetPropertyValue("TableComment", value);
            }
        }

        public virtual DateTime? CreateTime
        {
            get
            {
                return ((DateTime?)(base.GetPropertyValue("CreateTime")));
            }
            set
            {
                base.SetPropertyValue("CreateTime", value);
            }
        }

        public virtual DateTime? UpdateTime
        {
            get
            {
                return ((DateTime?)(base.GetPropertyValue("UpdateTime")));
            }
            set
            {
                base.SetPropertyValue("UpdateTime", value);
            }
        }

        public virtual ChangeType PendingChangeType
        {
            get
            {
                return ((ChangeType)(base.GetPropertyValue("PendingChangeType")));
            }
            set
            {
                base.SetPropertyValue("PendingChangeType", value);
            }
        }

        /// <summary>
        /// Whether this change is approved to be applied to the domain model
        /// </summary>
        public bool PendingChangeApproved
        {
            get { return Approved ?? false; }
            set { Approved = value; }
        }

        public virtual Boolean? Approved
        {
            get
            {
                return ((Boolean?)(base.GetPropertyValue("Approved")));
            }
            set
            {
                base.SetPropertyValue("Approved", value);
            }
        }
        #endregion
        #region Relationships
        public virtual BusinessObjectCollection<DBViewColumn> Columns
        {
            get
            {
                return Relationships.GetRelatedCollection<DBViewColumn>("Columns");
            }
        }
        public virtual IDBDatabase Database
        {
            get
            {
                return Relationships.GetRelatedObject<DBDatabase>("Database");
            }
            set
            {
                Relationships.SetRelatedObject("Database", value);
            }
        }
        #endregion
    }
}
